package com.bop.dehong.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.DeviceUuidFactory;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.StringUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.GetPhoneCodeModel;
import com.bop.dehong.http.model.RegisterModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.info.entity.RegistEntity;
import com.bop.dehong.webview.WebViewActivity;
import com.bop.dehong.widget.TimeButton;

public class RegisterActivity extends BaseActivity  {
	private EditText mUsername;
	private EditText mPhonecode, mInvitcode;
	private EditText mPassword, mPasswordAgain;
//	private TextView txt_tip;
	private TextView yewu_xieyi, user_xieyi;
	private RegisterModel mRegisterModel;
	private String mUserNameStr;
	private String mPasswordStr;
	private TimeButton mBtnPhoneCode;
	private GetPhoneCodeModel mGetPhoneCodeModel;
	private boolean isCheck = true;
	private CheckBox checkBox;

	@Override
	protected void initTop() {
		View mView = getView(R.id.register_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.register);
		manager.setLeftImageVisibility(View.VISIBLE);
	}
	/**
	 * 注册
	 */
	private void register(){
//		if (txt_tip.getVisibility() == View.VISIBLE) {
//			txt_tip.setVisibility(View.GONE);
//		}
		mUserNameStr = getStr4TextView(mUsername);
		mPasswordStr = getStr4TextView(mPassword);
		String confirPwdStr = getStr4TextView(mPasswordAgain);
		String phonecode = getStr4TextView(mPhonecode);
		String invit_code = getStr4TextView(mInvitcode);
		
		if (StringUtils.isEmpty(mUserNameStr)) {
			ToastUtils.show(this, "手机号码不能为空");
			return;
		}
		if (!RegexChk.checkCellPhone(mUserNameStr)) {
			ToastUtils.show(this, "手机号码格式不对");
			return;
		}
		if (StringUtils.isEmpty(mPasswordStr)) {
			ToastUtils.show(this, "密码不能为空");
			return;
		}
		if (!RegexChk.isPwd(mPasswordStr)) {
			ToastUtils.showLong(this, "密码由大小写字母及数字组合，6-18位字符");
//			txt_tip.setVisibility(View.VISIBLE);
			return;
		}
		if (TextUtils.isEmpty(confirPwdStr)) {
			ToastUtils.show(this, "确认密码不能为空!");
			return;
		}
		if (!mPasswordStr.equals(confirPwdStr)) {
			ToastUtils.show(this, "新密码与确定新密码不一致！");
			return;
		}
		if (StringUtils.isEmpty(phonecode)) {
			ToastUtils.show(this, "手机验证码不能为空");
			return;
		}
		if (phonecode.length() != 6) {
			ToastUtils.show(this, "手机验证码位数不对");
			return;
		}
		if (StringUtils.isEmpty(invit_code)) {
			invit_code = "";
		}
		
//			txt_tip.setVisibility(View.GONE);
			showProgressDialog();
			if (mRegisterModel == null ) {
				mRegisterModel = new RegisterModel(this, registerCallBack);
			}
			String device_code = "";
			device_code = JPushInterface.getRegistrationID(this);//传RegistrationID方便后台根据指定RegistrationID推送消息
			DeviceUuidFactory factory = new DeviceUuidFactory(this);
//			device_code = factory.getDeviceUuid() + "";
			LogUtils.e(TAG + ",device_code", "registrationId=" + device_code);
			String phone_type = factory.getPhoneType();
			mRegisterModel.doNet(mUserNameStr, mPasswordStr, invit_code, phonecode, device_code, phone_type);
	}
	
	RequestCallBack<RegistEntity> registerCallBack = new RequestCallBack<RegistEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(RegistEntity info) {
			// TODO Auto-generated method stub
			super.onSuccess(info);
			ToastUtils.show(RegisterActivity.this, "注册成功！");
			Intent intent1 = new Intent(RegisterActivity.this, MyIdentityActitvity.class);
			Bundle bundle = new Bundle();
			bundle.putString(Config.UID, info.getUid());
			bundle.putString("username", mUserNameStr);
			bundle.putString("password", mPasswordStr);
			intent1.putExtras(bundle);
			startActivityForResult(intent1, RESULT_FIRST_USER);
		}
		
	};
	
	/*
	 * 验证手机不判断密码
	 */
	private void getPhonecode(){
//		if (txt_tip.getVisibility() == View.VISIBLE) {
//			txt_tip.setVisibility(View.GONE);
//		}
		String username = getStr4TextView(mUsername);
		if (StringUtils.isEmpty(username)) {
			ToastUtils.show(this, "手机号码不能为空");
//			txt_tip.setVisibility(View.VISIBLE);
			return;
		}
		if (!RegexChk.checkCellPhone(username)) {
			ToastUtils.show(this, "手机号码格式不对");
//			txt_tip.setVisibility(View.VISIBLE);
			return;
		}
//		txt_tip.setVisibility(View.GONE);

		showProgressDialog();
		if (mGetPhoneCodeModel == null) {
			mGetPhoneCodeModel = new GetPhoneCodeModel(this, getPhoneCodeModelCallBack);
		}
		mGetPhoneCodeModel.doNet(username);
	}
	RequestCallBack<EmptyInfo> getPhoneCodeModelCallBack = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			// TODO Auto-generated method stub
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			// TODO Auto-generated method stub
			super.onSuccess(info);
			ToastUtils.show(RegisterActivity.this, "发送成功");
			mBtnPhoneCode.start();
		}
		
	};


	@Override
	protected void initUI() {
		mUsername = getView(R.id.register_username);
		mPhonecode = getView(R.id.register_phonecode);
//		txt_tip = getView(R.id.txt_tip);
		mPassword = getView(R.id.register_password);
		mPasswordAgain = getView(R.id.register_password_again);
		mInvitcode = getView(R.id.register_invite);
		mBtnPhoneCode = getView(R.id.register_btn_phonecode);
		yewu_xieyi = getView(R.id.yewu_xieyi);
		user_xieyi = getView(R.id.user_xieyi);
		checkBox = getView(R.id.login_ckb_remember);
		mBtnPhoneCode.setTextAfter("秒").setTextBefore("获取验证码").setLenght(60 * 1000);
		registerOnClickListener(getView(R.id.register_btn_register),
				 mBtnPhoneCode, yewu_xieyi, user_xieyi);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isCheck = isChecked;
			}
		});
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.activity_register;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK)
		{
			setResult(RESULT_OK, data);
			mBtnPhoneCode.onDestroy();
			finishCurrentActivity();
		}
		
	}
	
	@Override
	protected void onDestroy() {
		mBtnPhoneCode.onDestroy();
		super.onDestroy();
	}
	
	@Override
	public void onClick(View v)
	{
		Intent intent = null;
		String url = "";
		String title = "";
		switch (v.getId()) {
		case R.id.register_btn_register:
			if (isCheck) {
				register();	
			}else {
				ToastUtils.show(this, "请先勾选同意协议！");
			}
			break;
		case R.id.register_btn_phonecode:
			getPhonecode();
			break;
		case R.id.user_xieyi:
			url = "http://123.57.207.194:801/touch/app/yhxsxy/";
			LogUtils.e(TAG + ",yonghuyinsixieyi", url);
			title = "用户隐私协议";
			break;
		case R.id.yewu_xieyi:
			url = "http://123.57.207.194:801/touch/app/hyfwxy/";
			LogUtils.e(TAG + ", huiyuanfuwuxieyi", url);
			title = "德鸿金融会员服务协议";
			break;
		}
		if ( !TextUtils.isEmpty(url) || !TextUtils.isEmpty(title)) {
			intent = new Intent(this, WebViewActivity.class);
			intent.putExtra("url", url);
			intent.putExtra("title", title);
			intent.putExtra(Config.GO_BACK, true);
			startActivity(intent);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
}
